<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Crypt;
use Mail;
class loginController extends Controller
{
    public function index()
    {
    	return view('home.index.login',['title'=>'登录']);
    }
    public function login(Request $request)
    {
    	if ($request -> isMethod('post')) 
    	{
        		$code = $request ->input('Acode');
        		if ($code !=Session::get('code')) 
        		{
        			return back() -> with(['info'=>'验证码错误']);
        		}
    		$uname = $request -> input('uname');
    		$res = DB::table('users')->where('uname',$uname)->first();
            $time = Date('Y-m-d');
            $t = ['time'=>$time];
    		// dd($res);
            if (!$res) 
            {
                return back()->with(['info'=>'用户名未注册']);
            }  
    		if ($res->auth == 1 or $res->auth == 2) 
    		{
    			return back();
    		}
    		if (!$res) 
    		{
    			return back()->with(['info'=>'用户名未注册']);
    		}
    		$upwd = Crypt::decryptString($res->upwd);
    		if ($request->input('upwd') != $upwd) 
    		{
    			return back()->with(['info'=>'用户名或密码错误']);
    		}
    		if ($request->has('checkMe')) 
    		{
    			\Cookie::queue('isLogin',$res->upwd,60);
    		}
    		Session::put('homeM',$res);
            Session::put('time',$t);
            return redirect('/')->with(['info'=>'登录成功']);
    	}else
    	{
    		return view('home.index.login',['title'=>'登录']);
    	}
    }
    public function logout()
    {
    	Session::forget('homeM');
    	return redirect('/')->with(['info'=>'登出成功']);
    }
    public function register()
    {
    	return view('home.index.register',['title'=>'注册']);
    }
    public function insert(Request $request)
    {
       $this -> validate($request,[
              'uname'=>'required|min:3|max:18',
              'upwd'=>'required',
              'pepwd'=>'same:upwd',
              'tel'=>'digits:11',
              'email'=>'email'
         	],[
		        'uname.required' => '用户名不能为空最少3位最大18位',
		        'uname.min' =>'用户名最小为3位',
		        'uname.max' =>'用户名最大为18位',
		        'upwd.required' => '用户密码不能为空',
		        'tel.digits' => '手机号码必须为11位',
		        'email.email' => '请输入正确的邮箱格式'
        ]);

       if ($request->input('ed') == session('ed'))
       {
             $data = $request -> except('_token','pepwd','ed');
             $data['upwd'] = Crypt::encryptString($data['upwd']);//对字段进行加密
             //$wzq = Crypt::decryptString($str);//对字段进行解密
             $time = Date('Y-m-d H:i:s');
             $data['regtime'] = $time;
             $data['auth'] = 0;
             $data['email'] = session('email');
             $res = DB::table('users')->insert($data);
             // dd($res);
              if ($res) 
             {
              return redirect('/home/index/logon')->with(['info'=>'请登录']);
             }else
             {
              return back() -> with(['info'=>'添加失败']);
             }
       }
         
        
    }
}
